package models

import (
	//"github.com/astaxie/beego/logs"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"lottery/libs/mymgo"
	"lottery/utils"
	"time"
)

const (
	POLICY_COL_STR = "policy"
)

type Policy struct {
	Id     int64 `json:"id" bson:"_id"`
	UserId int64 `json:"userId" bson:"userId"`

	Platform1Id      int   `json:"platform1Id" bson:"platform1Id"`           // 平台1
	Platform1AgentId int64 `json:"platform1AgentId" bson:"platform1AgentId"` // 平台1账号ID
	Platform1GameId  int64 `json:"platform1GameId" bson:"platform1GameId"`   // 自己的GAME ID

	Platform2Id      int   `json:"platform2Id" bson:"platform2Id"`
	Platform2AgentId int64 `json:"platform2AgentId" bson:"platform2AgentId"` // 平台2账号ID
	Platform2GameId  int64 `json:"platform2GameId" bson:"platform2GameId"`   // 自己的GAME ID

	MinRate float64 `json:"minRate" bson:"minRate"` // 最低水位
	MaxRate float64 `json:"maxRate" bson:"maxRate"` // 最大水位
	Price   int     `json:"price" bson:"price"`     // 下单金额 单位分/人民币

	Status      int   `json:"status" bson:"status"`         // 0 暂停 1 开始 2 完成
	CreateTime  int64 `json:"createTime" bson:"createTime"` // 创建时间
	UpdateTime  int64 `json:"updateTime" bson:"updateTime"`
	IsDeleted   bool  `json:"isDeleted" bson:"isDeleted"`
	DeletedTime int64 `json:"deletedTime" bson:"deletedTime"`
}

type PolicyAttenData struct {
	Policy
	Agent1    *Agent    `json:"agent1"`
	Agent2    *Agent    `json:"agent2"`
	Game1     *Game     `json:"game1"`
	GameData1 *GameData `json:"gameData1"`
	Game2     *Game     `json:"game2"`
	GameData2 *GameData `json:"gameData2"`
	PageCount int       `json:"pageCount"`
}

func LoadPolicyByUserId(userId int64) (policys []*Policy, err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	policys = make([]*Policy, 0, 10)
	err = sess.WithC(POLICY_COL_STR, func(col *mgo.Collection) error {
		return col.Find(bson.M{
			"userId":    userId,
			"status":    1,
			"isDeleted": false,
		}).All(&policys)
	})
	return
}
func DeletePolicy(policyId int64) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	//假删除
	err = TearmDictDel(policyId)
	return sess.UpdateId(POLICY_COL_STR, policyId, bson.M{
		"$set": bson.M{
			"isDeleted": true,
		},
	})
}

//更新 策略列表 2小时内的

func UpdatePolicy(price int, userId int64) (err error) {

	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	times := utils.CurrentMills() - 200*60*1000
	return sess.UpdateAll(POLICY_COL_STR, bson.M{"userId": userId, "createTime": bson.M{"$gt": times}},
		bson.M{
			"$set": bson.M{
				"price": price,
			},
		})
}

func SavePolicy(policy *Policy, userId int64) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	policydata, err := GetPolicyData(userId)

	if err != nil {
		return
	}

	policy.MaxRate = policydata.MaxRate
	policy.MinRate = policydata.MinRate
	policy.Price = policydata.Price
	policy.UserId = userId
	policy.Status = 1
	policy.IsDeleted = false
	policy.CreateTime = time.Now().Unix()
	policy.Id = int64(sess.AutoIncId(POLICY_COL_STR))

	err = TearmDictAdd(policy)
	return sess.Insert(POLICY_COL_STR, policy)
}

// //组合信息 进行字典信息处理
func TearmDictAdd(policy *Policy) (err error) {

	platform1GameInfo, err := GetGameInfo(policy.Platform1Id, policy.Platform1GameId)
	platform2GameInfo, err := GetGameInfo(policy.Platform2Id, policy.Platform2GameId)
	if err != nil {
		return err
	}
	var tearm_dict *TearmDict
	//--- 球队1
	tearm_dict = new(TearmDict)
	tearm_dict.Platform1Id = policy.Platform1Id
	tearm_dict.Platform2Id = policy.Platform2Id
	tearm_dict.Platform1Tearm = platform1GameInfo.HName
	tearm_dict.Platform2Tearm = platform2GameInfo.HName
	err = SaveTearm(tearm_dict)
	//----球队2
	tearm_dict = new(TearmDict)
	tearm_dict.Platform1Id = policy.Platform1Id
	tearm_dict.Platform2Id = policy.Platform2Id
	tearm_dict.Platform1Tearm = platform1GameInfo.AName
	tearm_dict.Platform2Tearm = platform2GameInfo.AName
	err = SaveTearm(tearm_dict)
	return
}

//如果有赛事绑定被删除,有可能是绑定错了，要从字典里面删除
func TearmDictDel(policyId int64) (err error) {

	policy, err := GetpolicyById(policyId)
	if err != nil {
		return
	}
	platform1GameInfo, err := GetGameInfo(policy.Platform1Id, policy.Platform1GameId)
	platform2GameInfo, err := GetGameInfo(policy.Platform2Id, policy.Platform2GameId)
	if err != nil {
		return err
	}
	var tearm_dict *TearmDict

	//--- 球队1
	tearm_dict = new(TearmDict)
	tearm_dict.Platform1Id = policy.Platform1Id
	tearm_dict.Platform2Id = policy.Platform2Id
	tearm_dict.Platform1Tearm = platform1GameInfo.HName
	tearm_dict.Platform2Tearm = platform2GameInfo.HName
	err = DelTearmByTearm(tearm_dict)
	//----球队2
	tearm_dict = new(TearmDict)
	tearm_dict.Platform1Id = policy.Platform1Id
	tearm_dict.Platform2Id = policy.Platform2Id
	tearm_dict.Platform1Tearm = platform1GameInfo.AName
	tearm_dict.Platform2Tearm = platform2GameInfo.AName
	err = DelTearmByTearm(tearm_dict)
	return
}

func ExistsPolicy(policy *Policy, userId int64) (exist bool) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	return sess.Exist(POLICY_COL_STR, bson.M{
		"userId":          userId,
		"platform1Id":     policy.Platform1Id,
		"platform1GameId": policy.Platform1GameId,
		"platform2Id":     policy.Platform2Id,
		"platform2GameId": policy.Platform2GameId,
		"isDeleted":       false,
	})
}

func GetPolicyList(userId int64, page int, pagesize int) (policys []*Policy, pageCont int, err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	if pagesize == 0 {
		pagesize = 35
	}
	policys = make([]*Policy, 0, 100)
	//这里应该分页去取
	count := page * pagesize
	pageCont = sess.Count(POLICY_COL_STR, bson.M{"userId": userId})
	//err = sess.All(POLICY_COL_STR, bson.M{"userId": userId}, &policys)
	err = sess.WithC(POLICY_COL_STR, func(col *mgo.Collection) error {
		return col.Find(bson.M{"userId": userId, "isDeleted": false}).Sort("-createTime").Limit(pagesize).Skip(count).All(&policys)
	})
	return
}

func GetpolicyById(policyId int64) (policy *Policy, err error) {

	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	policy = new(Policy)
	err = sess.FindId(POLICY_COL_STR, policyId, policy)
	return
}
